/*
  Copyright (c) 2013 LEROY Anthony <leroy.anthony@gmail.com>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program. If not, see http://www.gnu.org/licenses/gpl.txt
*/

package fr.sigma.labo.autostoptan;

import android.os.AsyncTask;

import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import fr.sigma.labo.autostoptan.model.Data;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

/**
 * Created by aleroy on 19/06/13.
 */
public class GetDataTask extends AsyncTask<Void, Void, HttpResponse> {

    TextView textView;

    GetDataTask(TextView textView) {
        this.textView = textView;
    }

    @Override
    protected HttpResponse doInBackground(Void... jsonObjects) {
        HttpResponse response = null;

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
        HttpConnectionParams.setSoTimeout(httpParameters, 5000);

        HttpClient httpclient = new DefaultHttpClient(httpParameters);

        HttpGet httpget = new HttpGet("http://autostop.eu01.aws.af.cm/AutoStop/getAll");
        httpget.setHeader("Accept", "application/json");
        httpget.setHeader("Content-type", "application/json");

        try {
            // Execute HTTP Post Request
            System.out.println("Execute HTTP Get Request...");
            response = httpclient.execute(httpget);
            System.out.println("reponse: "+response.getEntity().toString());
        } catch (ClientProtocolException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }  catch (ConnectTimeoutException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }

        return response;
    }

    protected void onPostExecute(HttpResponse result) {
        if (result != null && result.getEntity() != null) {
            try {
                InputStream input = result.getEntity().getContent();
                // json is UTF-8 by default
                BufferedReader reader = new BufferedReader(new InputStreamReader(input, "UTF-8"), 8);
                StringBuilder sb = new StringBuilder();

                String line = null;
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }

                Gson gson = new Gson();
                Type collectionType = new TypeToken<Collection<Data>>(){}.getType();
                List<Data> d = gson.fromJson(sb.toString(),collectionType);
                for( Data d1 : d) {
                    System.out.println(d1.getId());
                    System.out.println(d1.getData());
                }

                textView.setText(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
